/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IsdComponent;

/**
 *
 * @author Shameera
 */
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.tree.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/** JTree with missing or custom icons at the tree nodes.
 *  1999 Marty Hall, http://www.apl.jhu.edu/~hall/java/
 */
public class CustomIcons extends JFrame {

    public static void main(String[] args) {
        try {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(CustomIcons.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(CustomIcons.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(CustomIcons.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(CustomIcons.class.getName()).log(Level.SEVERE, null, ex);
        }
        new CustomIcons();
    }
    private DefaultMutableTreeNode parentNode;
    private DefaultMutableTreeNode childNode;
//    private  DefaultMutableTreeNode grandChildNode;
    private final DefaultMutableTreeNode root;
    private ReadIsdXml readXml;

    public CustomIcons() {
        super("JTree Selections");
//    WindowUtilities.setNativeLookAndFeel();
//    addWindowListener(new ExitListener());
        Container content = getContentPane();
        content.setLayout(new FlowLayout());

        readXml = new ReadIsdXml("E:\\ISD_edited.xml");
        Node rootNode = readXml.getRootNode();
        root =
                new DefaultMutableTreeNode(rootNode.getNodeName());
        //    DefaultMutableTreeNode child;
//    DefaultMutableTreeNode grandChild;
//    for(int childIndex=1; childIndex<4; childIndex++) {
//      child = new DefaultMutableTreeNode("Child " + childIndex);
//      root.add(child);
//      for(int grandChildIndex=1; grandChildIndex<4; grandChildIndex++) {
//        grandChild =
//          new DefaultMutableTreeNode("Grandchild " + childIndex +
//                                     "." + grandChildIndex);
//        child.add(grandChild);
//      }
//    }
        // parentNode = root;
        childNode = root;
        createJTree(rootNode);
        this.setSize(500, 350);
        JTree tree1 = new JTree(root);
        tree1.expandRow(1); // Expand children to illustrate leaf icons
        JScrollPane pane1 = new JScrollPane(tree1);
        pane1.setSize(500, 350);
        pane1.setBorder(BorderFactory.createTitledBorder("Standard Icons"));
        content.add(pane1);
        pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        setVisible(true);
    }

    private void createJTree(Node node) {
        String value = null;
        Node tempNode = null;

        NodeList nodeList = node.getChildNodes();
        int nodeLength = nodeList.getLength();
        System.out.println(nodeList.getLength());
        if (nodeLength == 1) {
            tempNode = nodeList.item(0);
            value = tempNode.getNodeValue();
            if (!value.equals(null)) {
                DefaultMutableTreeNode grandChildNode1 = new DefaultMutableTreeNode(value);
                childNode.add(grandChildNode1);// add thid nodevlaue as a directory
                System.out.println(value);
                // add as an file in the directory
            }
//            createJTree(tempNode);

        } else if (nodeLength > 1) {
            for (int i = 1; i < nodeList.getLength(); i += 2) {
                tempNode = nodeList.item(i);
                DefaultMutableTreeNode grandChildNode2 = new DefaultMutableTreeNode(tempNode.getNodeName());
                System.out.println(tempNode.getNodeName());
                childNode.add(grandChildNode2);
                System.out.print("this is node :"+tempNode.getNodeName());
                System.out.print("----  this is treenode :"+ childNode.toString());
                System.out.println("this is treenode depth :"+childNode.getDepth());
                childNode = childNode.getLastLeaf();

                // System.out.println(tempNode.getNodeValue());
                createJTree(tempNode);
            }
            childNode = childNode.getPreviousNode();
        }
//        System.out.println(nodeList.getLength());
        childNode = childNode.getPreviousNode();
    }
}
